package mn.slide.laborberlin.adaptor;

import java.util.List;

import mn.slide.laborberlin.db.DataLayer;
import mn.slide.laborberlin.main.EintragActivity;
import mn.slide.laborberlin.main.R;
import mn.slide.laborberlin.model.Kapitel;
import mn.slide.laborberlin.tabgroup.HomeGroupActivity;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LeiExtendableAdapter extends BaseExpandableListAdapter
{

	private Context			myContext;

	private List<Kapitel>	item;
	static final String		groupItem[]	= { "Analysen nach Laborbereichen" };

	public LeiExtendableAdapter( Context context )
	{
		myContext = context;
		item = DataLayer.layer.getKapitelList();
	}

	@Override
	public Object getChild( int groupPosition , int childPosition )
	{
		return null;
	}

	@Override
	public long getChildId( int groupPosition , int childPosition )
	{
		return 0;
	}

	@Override
	public View getChildView( int groupPosition , final int childPosition , boolean isLastChild , View convertView , ViewGroup parent )
	{

		if (convertView == null)
		{
			LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.lei_child_row, null);
		}

		TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName);
		tvPlayerName.setText(Html.fromHtml(item.get(childPosition).getValue()));
		convertView.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick( View v )
			{
				// TODO Auto-generated method stub
				Intent intent = new Intent(myContext, EintragActivity.class);
				intent.putExtra("position", childPosition);
				HomeGroupActivity.group.goTo(intent);
			}
		});

		return convertView;
	}

	@Override
	public int getChildrenCount( int groupPosition )
	{
		return item.size() - 1;
	}

	@Override
	public Object getGroup( int groupPosition )
	{
		return null;
	}

	@Override
	public int getGroupCount()
	{
		return groupItem.length;
	}

	@Override
	public long getGroupId( int groupPosition )
	{
		return 0;
	}

	@Override
	public View getGroupView( int groupPosition , boolean isExpanded , View convertView , ViewGroup parent )
	{

		if (convertView == null)
		{
			LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.lei_group_row, null);
		}

		TextView tvGroupName = (TextView) convertView.findViewById(R.id.tvGroupName);
		tvGroupName.setText(groupItem[groupPosition]);
		ImageView view = (ImageView) convertView.findViewById(R.id.arrow);
		if (isExpanded)
		{
			view.setImageResource(R.drawable.arrowdown);
		}
		else
		{
			view.setImageResource(R.drawable.arrowright);
		}
		return convertView;
	}

	@Override
	public boolean hasStableIds()
	{
		return false;
	}

	@Override
	public boolean isChildSelectable( int groupPosition , int childPosition )
	{
		return true;
	}
}